import React, { Component } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';
import { Table, Divider } from 'antd';
// import Form from './components/SearchForm';
import { checkInit } from '@/utils/util';

export interface Props {
  dispatch?: Function,
  children?: React.ReactNode,
  loading?: boolean,
  list?: {
    list: Array<any>,
    total: number
  },
  buttons?: Array<any>,
  dict?: object,
  cond?: {
    pageNum: string,
    pageSize: string
  },
}

const ONE_PAGE_SIZE = 5;

@connect(({ analyse_check }) => ({
  list: analyse_check.list || {}
}))
class Tables extends Component<Props, any> {
  constructor(props) {
    super(props);
    this.initCurrent = this.initCurrent.bind(this);
  }
  state = {
    current: 1
  };

  queryData(current = 1) {
    const { dispatch } = this.props;

    dispatch({ type: 'analyse_check/queryPage', payload: { pageNum: current, pageSize: ONE_PAGE_SIZE } })
  }

  initCurrent() {
    this.setState({
      current: 1
    })
  }

  componentDidMount() {
    const { dispatch } = this.props;
    this.queryData();
  }

  render() {

    const { list = [], total } = this.props.list;
    const ans = list && Array.isArray(list) ? list.map((item, index) => ({
      key: index,
      id: item.id,
      name: item.name,
      startTime: item.startTime,
      endTime: item.endTime,
      })
    ) : [];
    const pagination = {
      total: total,
      showTotal: total => `共 ${total} 条`,
      showQuickJumper: true,
      pageSize: ONE_PAGE_SIZE,
      onChange: (current) => {
        this.setState({ current });
        this.queryData(current)
      },
    };
    const columns = [{
        title: '检查报告名称',
        dataIndex: 'name',
      }, {
        title: '开始时间',
        dataIndex: 'startTime',
      }, {
        title: '结束时间',
        dataIndex: 'endTime',
      }, {
        title: '操作', key: 'x', render: (record, text, index) => {
        return (
          <div style={{display: 'flex'}}>
            <Link to={`/data/analyse_check/${record.id}/details`}>详情</Link>
          </div>
        )}
      }];
    return (
      <div>
        {/* <Form current={ this.state.current } initCurrent={this.initCurrent} new={!!btnList['新增']}/> */}
        <Table
          columns={columns}
          pagination={pagination}
          dataSource={ans}
          bordered
          loading={this.props.loading}
        />
      </div>
    );
  }
}

export default Tables;
